package com.tools.cleanmaster.ui.card;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.tools.ad.EntranceType;
import com.tools.ad.util.GARecordUtils;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.common.thread.ThreadPool;
import com.tools.cleanmaster.controller.ITrashScanListener;
import com.tools.cleanmaster.controller.TrashHandler;
import com.tools.cleanmaster.manager.TrashManager;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.TrashesData;
import com.tools.cleanmaster.store.ResultPageSharedPrefManager;
import com.tools.cleanmaster.ui.activity.ImageBucketActivity;
import com.tools.cleanmaster.ui.activity.VideoLargeFileActivity;
import com.tools.cleanmaster.ui.card.factory.CardDataHelper;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.card.viewholder.ACardViewHolder;
import com.tools.cleanmaster.ui.card.viewholder.PhoneMediaViewHolder;
import com.tools.cleanmaster.ui.view.ToastHelper;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.StringUtils;

/**
 * Created by lianzhan on 15/12/21.
 */
public class PhoneStateCard extends ACardItem implements ITrashScanListener {
    private PhoneMediaViewHolder mPhoneMediaViewHolder;
    private boolean mFirstShow = true;
    private TrashHandler mTrashHandler;
    private int mType = -1;
    private boolean hasScanDone = false;

    public PhoneStateCard(EntranceType entranceType, int pos) {
        super(entranceType, pos);
    }

    @Override
    public CardViewType getCardViewType() {
        return CardViewType.PHONE_MEDIA;
    }

    @Override
    public boolean needShow(EntranceType entranceType) {
        boolean isShow = ResultPageSharedPrefManager.isCloudCardSwitch(imContext, getCardName());
        LogHelper.i(CardDataHelper.TAG, "Phone_deep item卡片开关=" + isShow);
        return isShow;
    }

    @Override
    public String getCardName() {
        return CardType.PHONE_MEDIA.key;
    }

    @Override
    public void bindCardView(final Activity activity, ACardViewHolder viewHolder, CardListAdapter cardAdapter,
                             final int position) {
        super.bindCardView(activity, viewHolder, cardAdapter, position);
        mPhoneMediaViewHolder = (PhoneMediaViewHolder) viewHolder;

        mPhoneMediaViewHolder.mImageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, ImageBucketActivity.class, VideoLargeFileActivity.IMAGE, position);
            }
        });

        mPhoneMediaViewHolder.mVideoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, VideoLargeFileActivity.class, VideoLargeFileActivity.VIDEO, position);
            }
        });
        mPhoneMediaViewHolder.mLargeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, VideoLargeFileActivity.class, VideoLargeFileActivity.LARGE, position);
            }
        });

        if (mFirstShow) {
            mFirstShow = false;
            mTrashHandler = TrashManager.getInstance().scan(this, true, false);
        }
        if (isReportShow) {
            isReportShow = false;
        }
    }

    private void doClick(Activity activity, Class c, int type, int position) {
        if (mPhoneMediaViewHolder == null) {
            return;
        }
        mType = type;
        Long size = null;
        if (type == VideoLargeFileActivity.IMAGE) {
            size = (Long) mPhoneMediaViewHolder.mImageSize.getTag();

            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    CleanGAConstant.L_CARD_IMAGES);

        } else if (type == VideoLargeFileActivity.VIDEO) {
            size = (Long) mPhoneMediaViewHolder.mVideoSize.getTag();

            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    CleanGAConstant.L_CARD_VIDEOS);
        } else if (type == VideoLargeFileActivity.LARGE) {
            size = (Long) mPhoneMediaViewHolder.mLargeSize.getTag();

            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    CleanGAConstant.L_CARD_LARGE_FILE);
        }
        if (size == null || size < 0) {
            ToastHelper.create(imContext, R.string.trash_clean_empty_summary, Toast.LENGTH_LONG).show();
            return;
        }

        Intent intent = new Intent(activity, c);
        if (type != VideoLargeFileActivity.IMAGE) {
            intent.putExtra(VideoLargeFileActivity.TYPE, type);
        }
        activity.startActivity(intent);
    }

    @Override
    public void onScanStart() {
        hasScanDone = false;
    }

    @Override
    public void onScanProgressUpdate(String desc, int percent, long size) {

    }

    @Override
    public void onScanTypeFinish(TrashType type, long size) {

    }

    @Override
    public void onScanFinish() {
        ThreadPool.runOnUi(new Runnable() {
            @Override
            public void run() {
                hasScanDone = true;
                setItemText();
            }
        });

    }

    private void setItemText() {
        long imageSize;
        long videoSize;
        long largeFileSize;

        if (mPhoneMediaViewHolder != null) {
            mPhoneMediaViewHolder.mImageProgress.setVisibility(View.GONE);
            mPhoneMediaViewHolder.mVideoProgress.setVisibility(View.GONE);
            mPhoneMediaViewHolder.mLargeProgress.setVisibility(View.GONE);

            mPhoneMediaViewHolder.mImageSize.setVisibility(View.VISIBLE);
            mPhoneMediaViewHolder.mVideoSize.setVisibility(View.VISIBLE);
            mPhoneMediaViewHolder.mLargeSize.setVisibility(View.VISIBLE);
        }

        TrashesData trashesData;
        if (mTrashHandler == null || !hasScanDone) {
            return;
        }

        trashesData = mTrashHandler.getTrashesData();
        if (trashesData == null) {
            return;
        }

        imageSize = trashesData.getSizeByType(TrashType.IMAGE_FILE);
        videoSize = trashesData.getSizeByType(TrashType.VIDEO_FILE);
        largeFileSize = trashesData.getSizeByType(TrashType.LARGE_FILE);

        mPhoneMediaViewHolder.mImageSize
                .setText(StringUtils.getSizeString(imageSize));
        mPhoneMediaViewHolder.mVideoSize
                .setText(StringUtils.getSizeString(videoSize));
        mPhoneMediaViewHolder.mLargeSize
                .setText(StringUtils.getSizeString(largeFileSize));

        if (imageSize > 0) {
            mPhoneMediaViewHolder.mImageSize.setTag(imageSize);
        } else {
            mPhoneMediaViewHolder.mImageSize.setTag(null);
        }

        if (videoSize > 0) {
            mPhoneMediaViewHolder.mVideoSize.setTag(videoSize);
        } else {
            mPhoneMediaViewHolder.mVideoSize.setTag(null);
        }

        if (largeFileSize > 0) {
            mPhoneMediaViewHolder.mLargeSize.setTag(largeFileSize);
        } else {
            mPhoneMediaViewHolder.mLargeSize.setTag(null);
        }
    }

    @Override
    public void onScanStop() {
        hasScanDone = true;
    }

    @Override
    public void onActivityResume() {
        super.onActivityResume();
        setItemText();
    }

    public int getClickType() {
        return mType;
    }

}
